package dao.impl;

import dao.PetDao;
import entity.Pet;
import entity.PetOwner;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class PetDaoImpl extends BaseDao implements PetDao {
    @Override
    public List<Pet> getPetList() throws Exception {
        connection=getConnection();
        String sql="select * from pet";
         resultSet = query(sql, null);
        ArrayList<Pet> list = new ArrayList<>();
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String typename = resultSet.getString("typename");
            int owner_id = resultSet.getInt("owner_id");
            int store_id = resultSet.getInt("store_id");
            int price = resultSet.getInt("price");

            Pet pet = new Pet();
            pet.setId(id);
            pet.setName(name);
            pet.setOwnerId(owner_id);
            pet.setStoreId(store_id);
            pet.setTypeName(typename);
            pet.setPrice(price);
            //添加到集合
            list.add(pet);
        }
        close(resultSet,statement,connection);
        return list;
    }

    @Override
    public List<Pet> getStorePetList() throws Exception{
        connection=getConnection();
        String sql="select * from pet p join petstore ps on p.store_id=ps.id";

        resultSet= query(sql, null);
        ArrayList<Pet> list = new ArrayList<>();
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String typename = resultSet.getString("typename");
            int price = resultSet.getInt("price");
            int store_id = resultSet.getInt("store_id");
            int owner_id = resultSet.getInt("owner_id");
            Pet pet = new Pet();
            pet.setId(id);
            pet.setName(name);
            pet.setTypeName(typename);
            pet.setPrice(price);
            pet.setStoreId(store_id);
            pet.setOwnerId(owner_id);
            list.add(pet);

        }

        close(resultSet,statement,connection);
        return list;
    }

    @Override
    public int getBuy(Integer petid, Integer ownerid) throws Exception{
        connection=getConnection();
        String sql="update pet set  store_id=?,owner_id=? where id=?";
        Object[] objects = new Object[3];
        objects[0]=null;
        objects[1]=ownerid;
        objects[2]=petid;
        int update = update(sql, objects);
        close(resultSet,statement,connection);
        return update;
    }

    @Override
    public List<Pet> ownerPet(PetOwner petOwner1) throws Exception{
        connection =getConnection();
        String sql="select * from pet where owner_id=?";
        Object[] objects={petOwner1.getId()};
        resultSet = query(sql, objects);
        ArrayList<Pet> list = new ArrayList<>();
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String typename = resultSet.getString("typename");
            int price = resultSet.getInt("price");
            int store_id = resultSet.getInt("store_id");
            int owner_id = resultSet.getInt("owner_id");
            Pet pet = new Pet();
            pet.setId(id);
            pet.setName(name);
            pet.setTypeName(typename);
            pet.setPrice(price);
            pet.setStoreId(store_id);
            pet.setOwnerId(owner_id);
            list.add(pet);
        }
        close(resultSet,statement,connection);
        return list;
    }

    @Override
    public int changePet(Integer petid, Integer ownerid) throws Exception {
        connection=getConnection();
        String sql="update pet set  store_id=?,owner_id=? where id=?";
        Object[] objects = new Object[3];
        objects[0]=ownerid;
        objects[1]=null;
        objects[2]=petid;
        int update = update(sql, objects);
        close(resultSet,statement,connection);
        return update;
    }

    /*@Override
    public int getBuy(int id, PetOwner petOwner1) throws Exception{
        connection=getConnection();
        String sql="update pet set store_id=?,owner_id=? where id=?";
        Object[] objects = new Object[3];
        objects[0]=null;
        objects[1]=petOwner1.getId();
        objects[2]=id;
        int update = update(sql, objects);
        if (update>0){
            int price = 0;
            int store_id=0;
            sql="select price,store_id from pet where id=?";
            Object[] objects2 = new Object[1];
            objects2[0]=id;
            resultSet = query(sql,objects2);
            while (resultSet.next()){
                price = resultSet.getInt("price");
                store_id=resultSet.getInt("store_id");
                System.out.println(price);
            }
            sql="update petowner set money=? where id=? and name=?";
            Object[] objects1 = new Object[3];
            objects1[0]=(petOwner1.getMoney()-price);
            objects1[1]=petOwner1.getId();
            objects1[2]=petOwner1.getName();
             update(sql, objects1);
             sql="update petsrore set balance=? where id=? ";
            Object[] objects3 = new Object[2];
           // objects3[0]=
        }


        close(resultSet,statement,connection);
        return update;
    }*/
}
